package cn.z2huo.usercenter.util;

import cn.z2huo.core.lang.Snowflake;
import cn.z2huo.core.lang.id.IdConstants;
import cn.z2huo.usercenter.constant.SystemConstants;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;

/**
 * <p>user-center 及子项目中使用 {@link SnowflakeGenerateUtils}，不使用 {@link cn.z2huo.core.util.IdUtils}
 *
 * @author z2huo
 */
@Getter
@RequiredArgsConstructor
public enum SnowflakeGenerateUtils {

    DEFAULT(""),

    TABLE_ID("tableId"),

    ;

    private final String key;

    private static final LocalDateTime LOCAL_DATE_TIME = LocalDateTime.of(2025, 1, 9, 16, 38, 17);

    public long nextId() {
        return getSnowflake(getKey()).nextId();
    }

    public String nextIdStr() {
        return getSnowflake(getKey()).nextIdStr();
    }

    public static Snowflake getSnowflake(Object... keys) {
        return cn.z2huo.core.util.IdUtils.getSnowflake(buildKey(keys), LOCAL_DATE_TIME, IdConstants.DEFAULT_WORKER_ID, IdConstants.DEFAULT_DATACENTER_ID, true);
    }

    private static String buildKey(Object... params) {
        if (ArrayUtils.isEmpty(params)) {
            return SystemConstants.CURRENT_SYSTEM_CODE;
        }
        return String.format("%s:%s", SystemConstants.CURRENT_SYSTEM_CODE, StringUtils.joinWith(":", params));
    }

}
